import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { STPage, STComponent, STColumn, STChange } from '@delon/abc/st';
import { SFSchema } from '@delon/form';
import { _HttpClient, ModalHelper } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { Api } from 'src/app/shared/api/api';
import { BaseAbilityComponent } from 'src/app/shared/base.ability.component';
import { DELETED_STATUS_BADGE, DELETED_STATUS_ENUM, PARAM_TYPE_ENUM, PARAM_TYPE_TAG } from 'src/app/shared/constant/common-constant';
import { ResponseCode } from 'src/app/shared/response.code';
import { AbilityService } from 'src/app/shared/service/ability.service';

import { ParamEditComponent } from './param-edit.component';

@Component({
  selector: 'app-system-param',
  templateUrl: './param.component.html'
})
export class SystemParamComponent extends BaseAbilityComponent implements OnInit, OnDestroy {
  params: any = {};
  searchParams: any = { deleted: false };
  page: any = {
    records: [],
    current: 1,
    total: 0,
    size: 10
  };
  pagination: STPage = {
    front: false,
    pageSizes: [10, 20, 30, 40, 50],
    total: true,
    showSize: true,
    showQuickJumper: true
  };
  searchSchema: SFSchema = {
    properties: {
      key: {
        type: 'string',
        title: '键',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      value: {
        type: 'string',
        title: '值',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      remark: {
        type: 'string',
        title: '描述',
        ui: {
          acl: { ability: ['query'] }
        }
      },
      type: {
        type: 'string',
        title: '类型',
        enum: PARAM_TYPE_ENUM,
        default: 0,
        ui: {
          widget: 'select',
          acl: { ability: ['query'] }
        }
      },
      deleted: {
        type: 'string',
        title: '状态',
        default: false,
        enum: DELETED_STATUS_ENUM,
        ui: {
          widget: 'select',
          acl: { ability: ['query'] }
        }
      }
    }
  };
  @ViewChild('st', { static: true }) st!: STComponent;
  columns: STColumn[] = [
    { title: '键', index: 'key' },
    { title: '值', index: 'value' },
    { title: '角色类型', index: 'type', type: 'tag', tag: PARAM_TYPE_TAG },
    { title: '描述', index: 'remark' },
    { title: '状态', index: 'deleted', type: 'badge', badge: DELETED_STATUS_BADGE },
    {
      title: '操作',
      buttons: [
        {
          text: '编辑',
          icon: 'edit',
          type: 'modal',
          modal: {
            component: ParamEditComponent
          },
          click: () => {
            this.query();
          },
          acl: { ability: ['edit'] }
        },
        {
          text: '删除',
          icon: 'delete',
          iif: record => record.type !== 5,
          click: (record: any) => {
            this.delete(record);
          },
          acl: { ability: ['delete'] }
        }
      ]
    }
  ];

  constructor(
    protected http: _HttpClient,
    private modal: ModalHelper,
    private modalService: NzModalService,
    private msg: NzMessageService,
    protected override route: ActivatedRoute,
    protected override ability: AbilityService
  ) {
    super(route, ability);
  }

  ngOnInit() {
    super.initAbilities();
    this.query();
  }

  ngOnDestroy(): void {
    super.clearAbilities();
  }

  change(e: STChange) {
    if (e.type === 'pi' || e.type === 'ps') {
      this.params.size = e.ps;
      this.params.current = e.pi;
      this.query();
    }
  }

  search(event: any) {
    if (event) {
      if (event.key) {
        this.searchParams.key = event.key;
      } else {
        delete this.searchParams.key;
      }
      if (event.value) {
        this.searchParams.value = event.value;
      } else {
        delete this.searchParams.value;
      }
      if (event.type) {
        this.searchParams.type = event.type;
      } else {
        delete this.searchParams.type;
      }
      if (event.remark) {
        this.searchParams.remark = event.remark;
      } else {
        delete this.searchParams.remark;
      }
      if (event.deleted !== null) {
        this.searchParams.deleted = event.deleted;
      } else {
        delete this.searchParams.deleted;
      }
      this.query();
    }
  }

  query() {
    const current: number = this.params.current || 1;
    const size: number = this.params.size || 10;
    this.http.get(`${Api.BaseSysParamApi}page/${current}/${size}`, this.searchParams).subscribe((res: any) => {
      if (res && res.code === ResponseCode.SUCCESS) {
        if (res.data) this.page = res.data;
      }
    });
  }

  add() {
    this.modal.createStatic(ParamEditComponent).subscribe(() => this.st.reload());
  }

  delete(record: any) {
    console.log(record);
    this.modalService.confirm({
      nzTitle: '确定删除吗?',
      nzContent: '<b style="color: red;">如果您确定要删除请点击确定按钮，否则点取消</b>',
      nzOkText: '确定',
      nzOkType: 'primary',
      nzOkDanger: true,
      nzOnOk: () =>
        this.http.delete(Api.BaseSysParamApi + record.id).subscribe((res: any) => {
          if (res) {
            if (res.code === ResponseCode.SUCCESS) {
              this.st.reload();
              this.msg.success('删除成功');
            } else {
              this.msg.warning(res.message);
            }
          } else {
            this.msg.error('删除失败，未知错误');
          }
        }),
      nzCancelText: '取消',
      nzOnCancel: () => console.log('Cancel')
    });
  }
}
